package com.sxpi.controller;

import com.sxpi.common.result.Result;
import com.sxpi.model.entity.Users;
import com.sxpi.model.page.PageResult;
import com.sxpi.model.vo.UsersVo;
import com.sxpi.service.UserService;
import com.sxpi.service.UsersService;
import com.sxpi.utils.FileUtil;
import com.sxpi.utils.MinioUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/users")
public class UsersController {
    @Resource
    private UsersService usersService;
    @Autowired
    private MinioUtils minioUtils;

    @Value("${uploadFile.path}")
    private String uploadDir;

    /**
     * 登录接口
     * @param loginParams 登录参数
     * @return 登录结果
     */
    @PostMapping("/login")
    public Result<UsersVo> login(@RequestBody Map<String, String> loginParams) {
        log.info("login params: {}", loginParams);
        return usersService.login(loginParams);
    }


       @PostMapping("/loginPhone")
        public Result<UsersVo> loginPhone(@PathVariable Long phone){
        return usersService.loginPhone(phone);
       }
    /**
     * 分页查询所有用户
     * @param pageNo 页码
     * @param pageSize 每页大小
     * @param nickName 昵称
     * @param phone 手机号
     * @param gender 性别
     * @return 用户列表
     */
    @GetMapping("/getAllUsers")
    public Result<PageResult<UsersVo>> getAllUsers(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                    @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                    @RequestParam(required = false) String nickName,
                                                    @RequestParam(required = false) String phone,
                                                    @RequestParam(required = false) String gender) {
        return usersService.getAllUsers(pageNo, pageSize, nickName, phone, gender);
    }

    /**
     * 更新用户信息
     * @param userId 用户ID
     * @param userInfo 用户信息，包含生日、性别、邮箱、电话、昵称等
     * @return 更新后的用户信息
     */
    @PostMapping("/information")
    public Result<UsersVo> updateUserInformation(@RequestParam("userId") Long userId, 
                                               @RequestBody Users userInfo) {
        return usersService.updateUserInformation(userId, userInfo);
    }

    /**
     * 上传用户头像
     * @param userId 用户ID
     * @param file 头像文件
     * @return 更新后的用户信息
     */
    @PostMapping("/avatar/{userId}")
    public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file,
                                        @PathVariable Long userId ) {

        
        return usersService.updateAvatar(userId, file);
    }

    /**
     * 获取用户详细信息，包括优惠券信息
     * @param userId 用户ID
     * @return 用户详细信息
     */
    @PostMapping("/me")
    public Result<Map> getUserDetailWithCoupons(@RequestParam("userId") Long userId) {
        return usersService.getUserDetailWithCoupons(userId);
    }



    @PostMapping("/upload")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) {
         String files = FileUtil.uploadFile(file, uploadDir);
        return Result.ok(files);
    }


}
